#!/bin/sh
#
# This is the script for generating files for a specific Dalton test job.
#
# For the .check file ksh or bash is preferred, otherwise use sh
# (and hope it is not the old Bourne shell, which will not work)
#
if [ -x /bin/ksh ]; then
   CHECK_SHELL='#!/bin/ksh'
elif [ -x /bin/bash ]; then
   CHECK_SHELL='#!/bin/bash'
else
   CHECK_SHELL='#!/bin/sh'
fi


#######################################################################
#  TEST DESCRIPTION
#######################################################################
cat > geoopt_prop2.info <<'%EOF%'
   geoopt_prop2
   ------------
   Molecule:         H2O
   Wave Function:    SCF / 6-311++G**
   Test Purpose:     Geometry optimization using .OPTIMIZE module
                     (2nd order) with a calculation of Third-Harmonic
                     generation at the optimized geometry
%EOF%

#######################################################################
#  MOLECULE INPUT
#######################################################################
cat > geoopt_prop2.mol <<'%EOF%'
BASIS
6-311++G**
Geometry optimization(2nd order), calculation of Third-Harmonic
generation at the optimized geometry
    2
        8.    1
O     0.0 -0.2249058930 0.0
        1.    2
H     1.45235 0.899623 0.0
H    -1.45235 0.899623 0.0
%EOF%

#######################################################################
#  DALTON INPUT
#######################################################################
cat > geoopt_prop2.dal <<'%EOF%'
**DALTON INPUT
.OPTIMIZE
*OPTIMIZE
.2NDORDER
**WAVE FUNCTION
.HF
**RESPONSE
*CUBIC
.THG
.DIPLEN
.FREQUE
 1
 0.0592
**END OF DALTON INPUT
%EOF%
#######################################################################



#######################################################################

#######################################################################
#  CHECK SCRIPT
#######################################################################
echo $CHECK_SHELL >geoopt_prop2.check
cat >>geoopt_prop2.check <<'%EOF%'
log=$1

if [ `uname` = Linux ]; then
   GREP="egrep -a"
else
   GREP="egrep"
fi

# Reading molecular geometry:
CRIT1=`$GREP "1 * x * (0| )\.0000000000" $log | wc -l`
CRIT2=`$GREP "2 * y * (0| )\.0000000000" $log | wc -l`
CRIT3=`$GREP "3 * z * (\-0|\-)\.1258515023" $log | wc -l`
CRIT4=`$GREP "4 * x * (0| )\.0000000000" $log | wc -l`
CRIT5=`$GREP "5 * y *  1\.4523500000" $log | wc -l`
CRIT6=`$GREP "6 * z * (0| )\.9986773907" $log | wc -l`
CRIT7=`$GREP "7 * x * (0| )\.0000000000" $log | wc -l`
CRIT8=`$GREP "8 * y * \-1\.4523500000" $log | wc -l`
CRIT9=`$GREP "9 * z * (0| )\.9986773907" $log | wc -l`
CRIT10=`$GREP "Total number of coordinates: * 9" $log | wc -l`
TEST[1]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4  \+ $CRIT5  \+ \
              $CRIT6  \+ $CRIT7  \+ $CRIT8  \+ $CRIT9  \+ $CRIT10`
CTRL[1]=10
ERROR[1]="MOLECULAR GEOMETRY NOT READ CORRECTLY"

# Check 2nd order geometry optimization:
CRIT1=`$GREP "2nd Order Geometry Optimization" $log | wc -l`
CRIT2=`$GREP "Newton method" $log | wc -l`
CRIT3=`$GREP "performed in Cartesian coordinates" $log | wc -l`
CRIT4=`$GREP "Trust region method will be used to control step" $log | wc -l`
TEST[2]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3  \+ $CRIT4`
CTRL[2]=4
ERROR[2]="GEOMETRY OPTIMIZATION NOT INITIALIZED CORRECTLY"

# Symmetry:
CRIT1=`$GREP "Number of coordinates in each symmetry: * 3 * 2 * 3 * 1" $log | wc -l`
CRIT2=`$GREP "Number of orbitals in each symmetry: * 17 * 6 * 11 * 2" $log | wc -l`
TEST[3]=`expr $CRIT1  \+ $CRIT2`
CTRL[3]=2
ERROR[3]="SYMMETRY IS NOT CORRECT"

# SCF energy, 1st iteration:
CRIT1=`$GREP "Total energy * \-76\.051459519[0-9] au \(Hartrees\)" $log | wc -l`
CRIT2=`$GREP "\-2069\.4654847[0-9] eV" $log | wc -l`
CRIT3=`$GREP "\-199673\.076[0-9] kJ\/mol" $log | wc -l`
TEST[4]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[4]=3
ERROR[4]="INITIAL ENERGY NOT CORRECT"

# Relativistic corrections:
CRIT1=`$GREP "Darwin correction\: *               0*\.1974514... au" $log | wc -l`
CRIT2=`$GREP "Mass\-velocity correction\: *     \-0*\.2489637... au" $log | wc -l`
CRIT3=`$GREP "Total relativistic correction\: * \-0*\.0515123... au \( *0*\.0677\%\)" $log | wc -l`
CRIT4=`$GREP "Non\-relativistic \+ relativistic energy\: * \-76\.1029718... au" $log | wc -l`
TEST[5]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4`
CTRL[5]=4
ERROR[5]="RELATIVISTIC CORRECTIONS NOT CORRECT"

# Gradient, 1st iteration:
CRIT1=`$GREP "O * z * \-0*\.04102" $log | wc -l`
CRIT2=`$GREP "H * y *   0*\.04951" $log | wc -l`
CRIT3=`$GREP "H * z *   0*\.04102" $log | wc -l`
TEST[6]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3`
CTRL[6]=3
ERROR[6]="INITIAL GRADIENT NOT CORRECT"

# Hessian, 1st iteration:
CRIT1=`$GREP "O * z * (0| )\.47225[0-9]" $log | wc -l`
CRIT2=`$GREP "H * y * (\-0|\-)\.36392[0-9] * (0| )\.78089[0-9]" $log | wc -l`
CRIT3=`$GREP "H * z * (\-0|\-)\.47225[0-9] * (0| )\.36392[0-9] * (0| )\.47225[0-9]" $log | wc -l`
CRIT4=`$GREP "O * x * (0| )\.03648[0-9]" $log | wc -l`
CRIT5=`$GREP "H * x * (\-0|\-)\.03648[0-9] * (0| )\.03648[0-9]" $log | wc -l`
CRIT6=`$GREP "O * y * (0| )\.65773[0-9]" $log | wc -l`
CRIT7=`$GREP "H * y * (\-0|\-)\.65773[0-9] * (0| )\.65773[0-9]" $log | wc -l`
CRIT8=`$GREP "H * z * (\-0|\-)\.48102[0-9] * (0| )\.48102[0-9] * (0| )\.4065[3-4][0-9]" $log | wc -l`
CRIT9=`$GREP "H * x * (0| )\.0340[8-9][0-9]" $log | wc -l`
TEST[7]=`expr $CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \
           \+ $CRIT6 \+ $CRIT7 \+ $CRIT8 \+ $CRIT9`
CTRL[7]=10
ERROR[7]="INITIAL HESSIAN NOT CORRECT"

# Dipole moment, 1st iteration:
CRIT1=`$GREP "0*\.88703. * 2\.25462. * 7\.5206" $log | wc -l`
TEST[8]=`expr $CRIT1`
CTRL[8]=1
ERROR[8]="INITIAL DIPOLE MOMENT NOT CORRECT"

# Step, 1st iteration:
CRIT1=`$GREP "O *      (0| )\.0000000000 *  0*\.0000000000 * \-0*\.0863" $log | wc -l`
CRIT2=`$GREP "H * _1 * (0| )\.0000000000 *   1\.41658..... *   0*\.97890" $log | wc -l`
CRIT3=`$GREP "H * _2 * (0| )\.0000000000 * \-1\.41658..... *   0*\.97890" $log | wc -l`
TEST[9]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[9]=3
ERROR[9]="INITIAL STEP NOT CORRECT"

# 2nd iteration:
CRIT1=`$GREP "Energy at this geometry is * \: * \-76\.05339" $log | wc -l`
CRIT2=`$GREP "Norm of gradient * \: * 0*\.00713" $log | wc -l`
CRIT3=`$GREP "Norm of step * \: * 0*\.0086" $log | wc -l`
TEST[10]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[10]=3
ERROR[10]="SECOND ITERATION NOT CORRECT"

# Final geometry:
CRIT1=`$GREP -l "O *      0*\.0000000000 *  0*\.0000000000    * \-0*\.08791" $log | wc -l`
CRIT2=`$GREP -l "H * _1 * 0*\.0000000000 *   1\.42252[45].... *   0*\.97971" $log | wc -l`
CRIT3=`$GREP -l "H * _2 * 0*\.0000000000 * \-1\.42252[45].... *   0*\.97971" $log | wc -l`
TEST[11]=`expr $CRIT1  \+ $CRIT2 \+ $CRIT3`
CTRL[11]=3
ERROR[11]="FINAL GEOMETRY NOT CORRECT"

# Final SCF energy:
CRIT1=`$GREP "Total energy * \-76\.0534235(4|5).[0-9] au \(Hartrees\)" $log | wc -l`
TEST[12]=`expr $CRIT1`
CTRL[12]=1
ERROR[12]="FINAL ENERGY NOT CORRECT"

# Gamma values:
CRIT1=`$GREP "\@ gamma\(X\;X\,X\,X\) *  1255\.3..." $log | wc -l`
CRIT2=`$GREP "\@ gamma\(X\;X\,Y\,Y\) *    37\.38.." $log | wc -l`
CRIT3=`$GREP "\@ gamma\(X\;Y\,Y\,X\) *    37\.38.." $log | wc -l`
CRIT4=`$GREP "\@ gamma\(X\;Y\,X\,Y\) *    37\.38.." $log | wc -l`
CRIT5=`$GREP "\@ gamma\(X\;X\,Z\,Z\) *   162\.53.." $log | wc -l`
CRIT6=`$GREP "\@ gamma\(X\;Z\,Z\,X\) *   162\.53.." $log | wc -l`
CRIT7=`$GREP "\@ gamma\(X\;Z\,X\,Z\) *   162\.53.." $log | wc -l`
CRIT8=`$GREP "\@ gamma\(Y\;Y\,X\,X\) *  \-21\.50.." $log | wc -l`
CRIT9=`$GREP "\@ gamma\(Y\;X\,X\,Y\) *  \-21\.50.." $log | wc -l`
CRIT10=`$GREP "\@ gamma\(Y\;X\,Y\,X\) * \-21\.50.." $log | wc -l`
CRIT11=`$GREP "\@ gamma\(Y\;Y\,Y\,Y\) *  402\.53.." $log | wc -l`
CRIT12=`$GREP "\@ gamma\(Y\;Y\,Z\,Z\) *  351\.60.." $log | wc -l`
CRIT13=`$GREP "\@ gamma\(Y\;Z\,Z\,Y\) *  351\.60.." $log | wc -l`
CRIT14=`$GREP "\@ gamma\(Y\;Z\,Y\,Z\) *  351\.60.." $log | wc -l`
CRIT15=`$GREP "\@ gamma\(Z\;Z\,X\,X\) *  100\.9..." $log | wc -l`
CRIT16=`$GREP "\@ gamma\(Z\;X\,X\,Z\) *  100\.9..." $log | wc -l`
CRIT17=`$GREP "\@ gamma\(Z\;X\,Z\,X\) *  100\.9..." $log | wc -l`
CRIT18=`$GREP "\@ gamma\(Z\;Z\,Y\,Y\) *  396\.57.." $log | wc -l`
CRIT19=`$GREP "\@ gamma\(Z\;Y\,Y\,Z\) *  396\.57.." $log | wc -l`
CRIT20=`$GREP "\@ gamma\(Z\;Y\,Z\,Y\) *  396\.57.." $log | wc -l`
CRIT21=`$GREP "\@ gamma\(Z\;Z\,Z\,Z\) *  407\.46.." $log | wc -l`
CRIT22=`$GREP "\@ Averaged gamma parallel to the applied field is * 618\.58...[0-9]" $log | wc -l`
TEST[13]=`expr	$CRIT1 \+ $CRIT2 \+ $CRIT3 \+ $CRIT4 \+ $CRIT5 \+ $CRIT6 \+ \
		$CRIT7 \+ $CRIT8 \+ $CRIT9 \+ $CRIT10 \+ $CRIT11 \+ $CRIT12 \+ \
		$CRIT13 \+ $CRIT14 \+ $CRIT15 \+ $CRIT16 \+ $CRIT17 \+ $CRIT18 \+ \
		$CRIT19 \+ $CRIT20 \+ $CRIT21 \+ $CRIT22`
CTRL[13]=22
ERROR[13]="GAMMA VALUES NOT CORRECT"

# Geometry optimization:
CRIT1=`$GREP "Geometry converged in * 3( |  )iterations\!" $log | wc -l`
CRIT2=`$GREP "Energy at final geometry is * \: * \-76\.053424 a\.u\." $log | wc -l`
CRIT3=`$GREP "Energy change during optimization \: * (\-0|\-)\.001964 a\.u\." $log | wc -l`
TEST[14]=`expr $CRIT1  \+ $CRIT2  \+ $CRIT3`
CTRL[14]=3
ERROR[14]="GEOMETRY OPTIMIZATION FAILED"

PASSED=1
for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14
do
   if [ ${TEST[i]} -ne ${CTRL[i]} ]; then
     echo "${ERROR[i]} ( test = ${TEST[i]}; control = ${CTRL[i]} )"
     PASSED=0
   fi
done

if [ $PASSED -eq 1 ]
then
  echo TEST ENDED PROPERLY
  exit 0
else
  echo THERE IS A PROBLEM
  exit 1
fi

%EOF%
#######################################################################
